package com.kum.server;

import com.github.monkeywie.proxyee.intercept.HttpProxyIntercept;
import com.github.monkeywie.proxyee.intercept.HttpProxyInterceptInitializer;
import com.github.monkeywie.proxyee.intercept.HttpProxyInterceptPipeline;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.net.InetSocketAddress;

/**
 * @version V1.0
 * @Package PACKAGE_NAME
 * @auhter 枯木Kum
 * @date 2021/6/15-4:02 PM
 * <p>...</p>
 */

@Component
public class ServerInterceptInitializer extends HttpProxyInterceptInitializer {

    @Autowired
    private MyFullResponseIntercept myFullResponseIntercept;


    @Override
    public void init(HttpProxyInterceptPipeline pipeline) {
//        System.out.println("线程Id: " + Thread.currentThread().getId());
        pipeline.addFirst(new HttpProxyIntercept(){
            @Override
            public void beforeRequest(Channel clientChannel, HttpRequest httpRequest, HttpProxyInterceptPipeline pipeline) throws Exception {
                InetSocketAddress inetSocketAddress = (InetSocketAddress) clientChannel.localAddress();
                httpRequest.headers().add("ip",inetSocketAddress.getHostString());
                pipeline.beforeRequest(clientChannel, httpRequest);
            }
        });
        pipeline.addLast(myFullResponseIntercept);
    }


}
